<template>
  <div class="app-container">
    <el-form ref="demo" :model="demo" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="8">
          <el-form-item prop="company" label="公司">
            <el-input v-model="demo.company" icon="search" name="title" placeholder="请输入公司名称" required/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item prop="displayName" label="负责人" label-width="80px">
            <el-input v-model="demo.displayName" icon="search" name="title" placeholder="请输入名字"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="cellphone" label="手机号" label-width="80px">
            <el-input v-model="demo.cellphone" icon="search" name="title" placeholder="请输入手机号"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="identityCard" label="身份证" label-width="80px">
            <el-input v-model="demo.identityCard" icon="search" name="title" placeholder="请输入手机号"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item prop="ord" label="属性">
            <el-input v-model="demo.ord" :disabled="true" icon="search" name="title" placeholder="请输入内容" value="代理商">代理商</el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item prop="province" label="授权区域" label-width="80px">
            <el-select v-model="demo.province" placeholder="请选择" style="width: 160px;" @change="cityShow">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="city" label="城市" label-width="80px">
            <el-select v-model="demo.city" placeholder="请选择" style="width: 160px;" @change="countyShow">
              <el-option
                v-for="item in citys"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item prop="activationMax" label="激活数量" label-width="80px">
            <el-input v-model="demo.activationMax" icon="search" name="title" type="number" placeholder="请输入数量"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="years" label="服务时间" label-width="80px">
            <el-input v-model="demo.years" icon="search" name="title" value="number" type="number" placeholder="请输月份"/>
            <!-- <el-select v-model="demo.years" placeholder="请选择" style="width: 160px;">
              <el-option
                v-for="item in years"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select> -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item prop="checkedCities" label="授权方案" label-width="80px">
        <el-checkbox-group v-model="demo.checkedCities">
          <el-checkbox v-for="city in cities" :label="city.key" :key="city.key">{{ city.value }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <!-- <el-row>
          <el-col :span="8">
          <el-form-item prop="username" label="后台账号" label-width="80px">
            <el-input v-model="demo.username" icon="search" name="title" placeholder="请输入名字"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="password" label="密码" label-width="80px">
            <el-input v-model="demo.password" icon="search" name="title" placeholder="请输入手机号"></el-input>
          </el-form-item>
        </el-col>
        </el-row> -->
    </el-form>
    <el-button type="primary" style="margin-top: 8px;margin-left: 20px;" @click="ServiceManagementAdd()">确定</el-button>
  </div>
</template>

<script>
import { fetchList } from "@/api/article";
import axios from "axios";
import api from "@/api/agent";
import { Message } from "element-ui";
const cityOptions = [];
export default {
  name: "InlineEditTable",
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      rules: {
        company: [{ required: true, message: "不能为空", trigger: "blur" }],
        displayName: [{ required: true, message: "不能为空", trigger: "blur" }],
        cellphone: [{ required: true, message: "不能为空", trigger: "blur" }],
        identityCard: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        province: [{ required: false, message: "不能为空", trigger: "change" }],
        city: [{ required: false, message: "不能为空", trigger: "change" }],
        activationMax: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        years: [{ required: true, message: "不能为空", trigger: "blur" }],
        checkedCities: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
        username: [{ required: true, message: "不能为空", trigger: "blur" }]
      },
      district: "",
      demo: {
        name: "",
        company: "",
        packageName: "",
        region: "",
        password: "123456",
        checkedCities: [],
        ord: "代理商",
        cellphone: "",
        identityCard: "",
        displayName: "",
        activationMax: "",
        years: "",
        username: ""
      },
      district: "",
      list: [],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      value: "",
      fileData: [],
      options: [],
      county: [],
      citys: [],
      checkAll: false,
      cities: cityOptions,
      isIndeterminate: true
    };
  },
  created() {},
  mounted() {
    api.regions().then(response => {
      // this.options = response.data
      response.map((item, index) => {
        this.options.push({
          label: item.label,
          value: item.code
        });
      });
    }),
      this.schemesShow();
    this.ServiceManagementShow();
  },
  methods: {
    schemesShow() {
      api.scheme().then(response => {
        cityOptions.splice(0, cityOptions.length);
        response.map((item, index) => {
          cityOptions.push({
            key: item.oid,
            value: item.name
          });
        });
      });
    },
    ServiceManagementShow() {
      const query = {};
      api.AgentManagementShow(this.$route.query.oid, query).then(response => {
        const data = response;
        console.log(data);
        // this.demo.username = data.username;
        this.demo.company = data.company;
        this.demo.activationMax = data.activationMax;
        this.demo.displayName = data.displayName;
        this.demo.cellphone = data.cellphone;
        this.demo.identityCard = data.identityCard;
        this.demo.province = data.province.code;
        this.demo.years = data.monthCount;
        const checkedCitiesData = [];
        data.schemes.map((item, index) => {
          checkedCitiesData.push(item.oid);
        });
        this.demo.checkedCities = checkedCitiesData;
        if (data.province.code) {
          api.city(data.province.code).then(response => {
            response.map((item, index) => {
              this.citys.push({
                label: item.label,
                value: item.code
              });
            });
            if (data.city.code) {
              console.log(data.city.code);
              this.demo.city = data.city.code;
            }
          });
        }
      });
    },
    ServiceManagementAdd() {
      this.$refs["demo"].validate(valid => {
        if (valid) {
          const checkgroup = [];
          this.demo.checkedCities.map((item, index) => {
            checkgroup.push({ oid: Number(item) });
          });
          const data = {
            activationMax: this.demo.activationMax,
            company: this.demo.company,
            displayName: this.demo.displayName,
            cellphone: this.demo.cellphone,
            identityCard: this.demo.identityCard,
            province: {
              code: this.demo.province
            },
            city: {
              code: this.demo.city
            },
            schemes: checkgroup,
            monthCount: 5,
            username: this.demo.username,
            password: this.demo.password,
            oid: Number(this.$route.query.oid)
          };
          api.AgentManagementEdit(data).then(({ success, data }) => {
            this.$message({
              showClose: true,
              message: "编辑成功！",
              type: "success"
            });
            this.$router.push("/AgentManagement");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    cityShow(value, data) {
      api.city(value, data).then(response => {
        if (this.citys.length > 0) {
          this.citys = [];
          this.demo.city = "请选择";
        }
        if (this.county.length > 0) {
          this.county = [];
          this.demo.county = "请选择";
        }
        response.map((item, index) => {
          this.citys.push({
            label: item.label,
            value: item.code
          });
        });
      });
    },
    countyShow(value, data) {
      api.county(value, data).then(response => {
        if (this.county.length > 0) {
          this.county = [];
          this.demo.county = "请选择";
        }
        response.map((item, index) => {
          this.county.push({
            label: item.label,
            value: item.code
          });
        });
      });
    }
  }
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
#upClass {
  width: 420px;
  height: 35px;
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  line-height: 40px;
  outline: 0;
  padding: 0 15px;
  -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
